from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
import time
class Base():

    #初始化
    def __init__(self,driver):
        self.driver=driver
    #打开网址
    def open_url(self,url):
        self.driver.get(url)

    #显示等待查找元素
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver : driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            t=time.strftime('%Y%m%d-%H%M%S',time.localtime())
            self.driver.save_screenshot(t+'.png')
            print(f"元素定位失败{e}")
            return None
    #点击
    def click(self,loc):
        self.find_element(loc).click()
    #输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    #文本
    def text(self,loc):
        return self.find_element(loc).text
    #清空
    def clear(self,loc):
        self.find_element(loc).clear()

    #3.封装技术点，滚动条（js注入）、alert弹窗处理、iframe 切换、窗口切换、
    #鼠标事件、键盘事件、浏览器窗口操作、下拉框选择，即使部分功能用例用不到，也要保证方法可调用

    #切换新窗口
    def handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    #进入frame框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    #退出frame框架
    def content(self):
        self.driver.switch_to.default_content()

    #滚动条事件
    def scroll(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

    #alert弹窗处理
    def accept(self):
        alert=self.driver.switch_to.alert
        alert.accept()
    def dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()
    def send_accept(self,info):
        alert = self.driver.switch_to.alert
        alert.send_keys(info)
        alert.accept()
    def send_dismiss(self,info):
        alert = self.driver.switch_to.alert
        alert.send_keys(info)
        alert.dismiss()

    #下拉框选择
    def select_index(self,loc,x):
        index=Select(self.find_element(loc))
        index.select_by_index(x)

    def select_value(self,loc,x):
        value=Select(self.find_element(loc))
        value.select_by_value(x)

    def select_text(self,loc,info):
        s_text=Select(self.find_element(loc))
        s_text.select_by_visible_text(info)

    #鼠标事件
    def a_double(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    def a_move_to(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    def a_context(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()
    def a_hold(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()
    def three(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click().perform()

    #键盘事件
    def enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    def tab(self,loc):
        self.find_element(loc).send_keys(Keys.TAB)
    def space(self,loc):
        self.find_element(loc).send_keys(Keys.SPACE)
    def backspace(self,loc):
        self.find_element(loc).send_keys(Keys.BACKSPACE)
    def ctrl(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL)

    #浏览器刷新
    def refresh(self):
        self.driver.refresh()
    #浏览器关闭窗口
    def close(self):
        self.driver.close()






